const LOGIN_NAME = 1;
const LOGIN_PWD  = 2;
const ERROR_TIPS_NAME = '请输入正确的手机号码';
const ERROR_TIPS_PWD = '请输入正确的登录密码';


$(()=>{
    var boot = new Boot();
    boot.init();
})

//启动时执行
function Boot(){
    this.init = ()=>{
        var mad = new Ad(2,500);
        mad.init();
        this.bind();
    };
    this.bind = ()=>{
        $("#mobile").blur((e)=>{
            let val = $(e.target).val();
            let item =  $(e.target);
            let tips = new ContentTips(LOGIN_NAME,val,item,ERROR_TIPS_NAME);
            tips.contentCheck();
        });
        $("#pwd").blur((e)=>{
            console.log("pwd");
            let val = $(e.target).val();
            let item =  $(e.target);
            let tips = new ContentTips(LOGIN_PWD,val,item,ERROR_TIPS_PWD);
            tips.contentCheck();
        });
        $(".login-content .panel button").click(()=>{
            let _mobileVal = $("#mobile").val();
            let _pwdVal = $("#pwd").val();
            let v1 = new Verify(LOGIN_NAME,_mobileVal);
            let isMoibleOk = v1.check();
            let v2 = new Verify(LOGIN_PWD,_pwdVal);
            let isPwdOk = v2.check();
            if(isPwdOk && isMoibleOk){
                $(".msg-tip").animate({'right':'-50%'},300);
                window.location.href="index.html";
            }else{
                $(".msg-tip").animate({'right':'0'},300);
            }
            
        })
    }
}

/**
 * 广告倒计时
 * initNum 倒计时初始值
 * step 间隔执行时间(毫秒)
*/function  Ad(initNum,step){
    let timer;
    this.step = step;
    this.initNum = initNum;
    this.init = ()=>{
        $("#count-down span b").text(initNum);
        this.count(LOGIN_NAME,);
    };
    this.count = ()=>{
        var nowNum = $("#count-down span b").text();
        timer = setInterval(()=>{
            if(nowNum > 0){
                nowNum --;
                $("#count-down span b").text(nowNum);
                console.log(nowNum,);
            }else{
                clearInterval(timer);
                this.jump();
            }
        },step);
    };
    this.jump = ()=>{
        $("#count-down").fadeOut(500);
    }
}


//正则验证
function Verify(type,val){
    this.type = type;
    this.val = val;

    this.check = ()=>{
        let filter_pwd  = /^[a-zA-Z]\w{5,17}$/;  
        let filter_name  = /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
        switch(type){
            case LOGIN_NAME:{
                return filter_name.test(val);
            };
            case LOGIN_PWD:{
                return filter_pwd.test(val);
            };
            default:{
                console.log("传值不正确,type="+type+",val="+val);
                return false;
            }
        }
    };

}

//内容验证和提示信息
function ContentTips(type,val,curr,error){
    Verify.call(this,type,val);
    this.show = ()=>{
        curr.siblings(".error-tip").slideDown().text(error);
    };
    this.hide = ()=>{
        curr.siblings(".error-tip").slideUp();
    };
    this.contentCheck = ()=>{
        if(!this.check()){
            this.show();
        }else{
            this.hide();
        }
    };
}

